
#include "eflash.h"
#include "main.h"

u8 flash_data_buf[100] __attribute__((aligned(4)));
/**
  * @brief  eflash initialization function
  * @param  None
  * @retval None
  */
void eflash_init(void)
{
    TYPE_LL_EFLASH_INIT eflash_init;
    memset(&eflash_init, 0x00, sizeof(eflash_init));
    
    /* Initialize the relevant variables  */
    ll_eflash_init(EFLASH, &eflash_init);
}

#if 0
/**
  * @brief  eflash data handle function eFlash 数据处理功能
  * @param  None
  * @retval None
  */
void eflash_handle(void)
{
    u32 i = 0;
    u32 *pBuf = NULL;
    u8 eflash_data_sec_index = 61;      //
    u32 eflash_addr = eflash_data_sec_index * LL_EF_SECTOR_SIZE;
    pBuf = (u32 *)&flash_data_buf[0];
//    printf("eflash program begin \r\n");
    memset(flash_data_buf, 0x00,sizeof(flash_data_buf));
    for(i = 0; i < (sizeof(flash_data_buf)); i += 4)
    {
        *pBuf++ = LL_EFLASH_DATA_GET(eflash_addr + i);
    }
    printf("flash begin read:%d bytes\r\n", sizeof(flash_data_buf));
    printf_array(flash_data_buf, sizeof(flash_data_buf));
    printf("\r\n");

    ll_eflash_erase_one_sector(EFLASH, eflash_data_sec_index);
    memset(flash_data_buf, 0x00,sizeof(flash_data_buf));
    for(i = 0; i < LL_EF_SECTOR_SIZE; i += 4)
    {
        if(0xFFFFFFFF != LL_EFLASH_DATA_GET(eflash_addr + i))
        {
            printf("eflash_erase verify failed\r\n");
            break;
        }
    }
    if(i >= LL_EF_SECTOR_SIZE)
    {
        printf("eflash erase ok\r\n");
        for(i = 0; i < sizeof(flash_data_buf); i++)
        {
            flash_data_buf[i] = i;
        }
        pBuf = (u32 *)&flash_data_buf[0];
        for(i = 0; i < (sizeof(flash_data_buf)); i += 4)
        {
            ll_eflash_prog_one_data(EFLASH, eflash_addr + i, *pBuf++);
        }
        printf("flash write:%d bytes\r\n", sizeof(flash_data_buf));
        printf_array(flash_data_buf, sizeof(flash_data_buf));
        printf("\r\n");
        

        memset(flash_data_buf, 0x00, sizeof(flash_data_buf));
        pBuf = (u32 *)&flash_data_buf[0];
        for(i = 0; i < (sizeof(flash_data_buf)); i += 4)
        {
            *pBuf++ = LL_EFLASH_DATA_GET(eflash_addr + i);
        }
        printf("flash read after write:%d bytes\r\n", sizeof(flash_data_buf));
        printf_array(flash_data_buf, sizeof(flash_data_buf));
        printf("\r\n");
    }
}
#endif

#define eflash_data_sec_index      61
#define eflash_addr  eflash_data_sec_index * LL_EF_SECTOR_SIZE

void eflash_save(void * data,int16_t data_len)
{
    uint8_t i;
    u32 *pBuf = NULL;
    ll_eflash_erase_one_sector(EFLASH, eflash_data_sec_index);
    memset(flash_data_buf, 0x00,sizeof(flash_data_buf));
    for(i = 0; i < LL_EF_SECTOR_SIZE; i += 4)
    {
        if(0xFFFFFFFF != LL_EFLASH_DATA_GET(eflash_addr + i))
        {
            printf("eflash_erase verify failed\r\n");
            break;
        }
    }
    if(i >= LL_EF_SECTOR_SIZE)
    {
        printf("eflash erase ok\r\n");
        for(i = 0; i < sizeof(flash_data_buf); i++)
        {
            flash_data_buf[i] = i;
        }
        pBuf = (u32 *)data;
        for(i = 0; i < (sizeof(flash_data_buf)); i += 4)
        {
            ll_eflash_prog_one_data(EFLASH, eflash_addr + i, *pBuf++);
        }
        printf("flash write:%d bytes\r\n", sizeof(flash_data_buf));
        printf_array(flash_data_buf, sizeof(flash_data_buf));
        printf("\r\n");
    }
}

void eflash_read(void * data,int16_t data_len)
{
    uint8_t i;
    u32 *pBuf = NULL;
    pBuf = (u32 *)data;
    for(i = 0; i < (data_len); i += 4)
    {
        *pBuf++ = LL_EFLASH_DATA_GET(eflash_addr + i);
    }
    printf("flash begin read:%d bytes\r\n", data_len);
    printf_array(flash_data_buf, data_len);
    printf("\r\n");
}
